package com.geek.service.impl;


import com.geek.bean.PageData;
import com.geek.bean.vo.FootprintVo;
import com.geek.mapper.MarketFootprintMapper;
import com.geek.mapper.MarketGoodsMapper;
import com.geek.model.MarketFootprint;
import com.geek.model.MarketFootprintExample;
import com.geek.model.MarketGoods;
import com.geek.service.FootprintService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    MarketFootprintMapper footprintMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;



    @Override
    public PageData list(int userId, int page, int limit) {
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        marketFootprintExample.setOrderByClause("add_time desc");
        marketFootprintExample.createCriteria()
                .andUserIdEqualTo(userId)
                .andDeletedEqualTo(false);
        //查询足迹
        List<MarketFootprint> footprintList = footprintMapper.selectByExample(marketFootprintExample);
        PageHelper.startPage(page, limit);
        //存放足迹
        List<FootprintVo> footprintVoList= new ArrayList<>();
        for (MarketFootprint marketFootprint : footprintList) {
            if (marketFootprint != null) {
                FootprintVo footprintVo = new FootprintVo();
                MarketGoods good = marketGoodsMapper.selectByPrimaryKey(marketFootprint.getGoodsId());
                if (good != null) {
                    footprintVo.setBrief(good.getBrief());
                    footprintVo.setPicUrl(good.getPicUrl());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    footprintVo.setAddTime(sdf.format(marketFootprint.getAddTime()));
                    footprintVo.setGoodsId(good.getId());
                    footprintVo.setName(good.getName());
                    footprintVo.setId(marketFootprint.getId());
                    footprintVo.setRetailPrice(good.getRetailPrice());

                    footprintVoList.add(footprintVo);
                }
            }
        }
        return PageData.list(footprintVoList);

    }

    @Override
    public int delete(Integer id) {
        MarketFootprint marketFootprint = new MarketFootprint();
         marketFootprint.setId(id);
        marketFootprint.setDeleted(true);
        marketFootprint.setUpdateTime(new Date());
        return footprintMapper.updateByPrimaryKeySelective(marketFootprint);
    }

}
